package com.guodi.map.controller;

import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapFunction;
import com.guodi.map.service.IMapCommonService;
import com.guodi.map.service.IMapFunctionAuthService;
import com.guodi.map.service.IMapFunctionDirService;
import com.guodi.map.service.IMapFunctionService;
import com.guodi.map.utils.VerificationUtil;
import com.guodi.map.vo.ApiMapFunction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @描述：地图功能模块表控制类
 * @作者: 代鹏刚
 * @日期: 2019-12-13
 */
@Validated
@RestController
@RequestMapping("/MapFunction")
@Api(value = "API - MapFunctionController", tags = "地图功能模块表")
public class MapFunctionController extends BaseController {

    @Autowired
    private IMapFunctionService mapFunctionService;
    @Autowired
    private IMapFunctionDirService mapFunctionDirService;
    @Autowired
    private IMapFunctionAuthService mapFunctionAuthService;
    @Autowired
    private IMapCommonService commonService;

    /**
    * @描述: 根据实体属性查询
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2019-12-13
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性查询")
    @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "功能名称", required = false, dataType = "string", paramType = "query"))
    public R listByEntity(@ApiIgnore MapFunction mapFunction){
        List<MapFunction> list = mapFunctionService.listByEntity(mapFunction);
        return R.data(list);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R findById(String id) {
        if(StringUtils.isBlank(id)){
            return R.fail(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MapFunction mapFunction = mapFunctionService.getById(id);
        return R.data(mapFunction);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resourceId", value = "资源目录ID", required = true, dataType = "string", paramType = "query")
    })
    public R save(@Valid ApiMapFunction apiMapFunction, String resourceId){
        MapFunction mapFunction = new MapFunction();
        BeanUtils.copyProperties(apiMapFunction,mapFunction);
        if(!prepareData(mapFunction, true)){
            return R.fail(StatusCode.ERROR_CODE_40005,"该功能编码已经存在，请更换！");
        }
        mapFunction.setId(UuidUtil.get32UUID());
        if(null == mapFunction.getSort()){
            //设置排序
            Integer sort = commonService.getMaxSort(null,"map_function");
            mapFunction.setSort(sort + 1);
        }
        mapFunctionService.save(mapFunction);
        return R.data(mapFunction);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(MapFunction mapFunction) {
        if(!prepareData(mapFunction, false)){
            return R.fail(StatusCode.ERROR_CODE_40005,"该功能编码已经存在，请更换！");
        }
        mapFunctionService.updateById(mapFunction);
        return R.data(mapFunction);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    @Transactional(rollbackFor = Exception.class)
    public R delete(String id){
        mapFunctionService.removeById(id);
        //删除关联数据
        Map map = new HashMap();
        map.put("function_id",id);
        mapFunctionDirService.removeByMap(map);
        mapFunctionAuthService.removeByMap(map);
        return R.data(null);
    }

    private boolean prepareData(MapFunction mapFunction, boolean isSave) {
        //验证是否重名
        List<MapFunction> list = (List<MapFunction>)mapFunctionService.listByMap(VerificationUtil.verifyMapByCode(mapFunction.getCode(),null));
        if(isSave){
            if(list.size() > 0){
                return false;
            }
            mapFunction.setCreator(getUser().getUserId().toString());
            mapFunction.setCreateTime(new Date());
            //防止误修改
            mapFunction.setModifier(null);
            mapFunction.setModifyTime(null);
            return true;
        }else{
            if(list.size() > 0 && !list.get(0).getId().equals(mapFunction.getId())){
                return false;
            }
            mapFunction.setModifier(getUser().getUserId().toString());
            mapFunction.setModifyTime(new Date());
            //防止误修改
            mapFunction.setCreator(null);
            mapFunction.setCreateTime(null);
            return true;
        }
    }
}
